﻿using System;
using System.Windows.Forms;
using ReAnimation.DAL.RepositoryInterfaces;
using ReAnimation.Domain;
using ReAnimation.Graphics;
using ReAnimation.Graphics.Handlers.TwoAtomsSelect;
using ReAnimation.Helpers.Common;
using ReAnimation.Helpers.IoC;
using ReAnimation.Helpers.WinForms;
using ReAnimation.MVP.Models.Interfaces;
using ReAnimation.MVP.Presenters;
using ReAnimation.MVP.ViewInterfaces;
using ReAnimation.Graphics.Handlers;
using System.Linq.Expressions;

namespace ReAnimation.MVP.Views
{
    public partial class MainForm : BaseFormView,IMainView
    {
        private readonly MainPresenter _mainFormPresenter;
        private readonly ISceneManipulationView _sceneManipulationView;
        private readonly IAnimationProjectSettingsView _animationProjectSettingsView;
        private readonly IMoleculesDisplayView _moleculesDisplayView;

        public MainForm(MainPresenter mainFormPresenter,
                        ISceneManipulationView sceneManipulationView,
                        IAnimationProjectSettingsView animationProjectSettingsView,
                        IMoleculesDisplayView moleculesDisplayView)
        {
            _mainFormPresenter = mainFormPresenter;
            _sceneManipulationView = sceneManipulationView;
            _animationProjectSettingsView = animationProjectSettingsView;
            _moleculesDisplayView = moleculesDisplayView;

            _mainFormPresenter.View = this;

            InternalInitializeComponent();
         /*   _moleculesDisplayView = moleculesDisplayView;
            _zMatrixTable = zMatrixTable; 
            _mainModel = mainModel;
            _twoAtomsSelectHandler = twoAtomsSelectHandler;
            _moleculeTransformHandler = moleculeTransformHandler;
            _mainPresentationModel = mainPresentationModel;
            _animationStepsView = animationStepsView;
            _mainFormPresenter = mainFormPresenter;
            _linkingDialogViewInjector = linkingDialogViewInjector;
            _atomDescriptionRepository = atomDescriptionRepository;

           // _twoAtomsSelectHandler.MoleculeDrawer = _moleculeDrawer;

           

            _mainModel.CurrentMoleculeChanged += CurrentMoleculeChangedHandler;
            
            _mainFormPresenter.View = this;

            _mainPresentationModel.LinkingModeChanged += _mainPresentationModel_LinkingModeChanged;
            _twoAtomsSelectHandler.Selected += _twoAtomsSelectHandler_Selected;
            _mainModel.MoleculesCollectionChanged += MainModelOnMoleculesCollectionChanged;*/
        }

        private void MainModelOnMoleculesCollectionChanged(object sender, EventArgs eventArgs)
        {
            //currentMoleculeComboBox.DataSource = _mainModel.Molecules;
        }

        private void InternalInitializeComponent()
        {
            InitializeComponent();

            var sceneManipulationControl = _sceneManipulationView as UserControl;
            sceneManipulationControl.Dock = DockStyle.Fill;

            bottomPanel.Controls.Add(sceneManipulationControl);

            var animationProjectSettingsControl = _animationProjectSettingsView as UserControl;
            animationProjectSettingsControl.Dock = DockStyle.Fill;

            mainSplitContainer.Panel1.Controls.Add(animationProjectSettingsControl);

            var moleculesDisplayControl = _moleculesDisplayView as UserControl;
            moleculesDisplayControl.Dock = DockStyle.Fill;

            mainSplitContainer.Panel2.Controls.Add(moleculesDisplayControl);
        }

        void _twoAtomsSelectHandler_Selected(object sender, EventArg<Pair<Atom, Atom>> e)
        {
            /*
            _mainPresentationModel.CurrentlySelectedForLinkingAtoms = e.Data;
            LinkingAtomsPairSelected.Throw(this,EventArgs.Empty);*/
        }

        void _mainPresentationModel_LinkingModeChanged(object sender, EventArg<bool> e)
        {
          /*  if (e.Data)
            {
                linkingModeButton.Text = "End linking";
                _twoAtomsSelectHandler.StartHandle();
            }
            else
            {
                linkingModeButton.Text = "Start linking";
                _twoAtomsSelectHandler.StopHandle();
            }*/
        }

        void CurrentMoleculeChangedHandler(object sender, EventArgs e)
        {
           // currentMoleculeComboBox.SelectedItem = _mainModel.CurrentMolecule;
        }

        public event EventHandler LinkingAtomsPairSelected;
        public event EventHandler CreateNewMolecule;
        public event EventHandler LoadMolecule;
        public event EventHandler CreateNewAnimation;
        public event EventHandler LoadAnimation;
        public event EventHandler SaveAnimation;
        public event EventHandler DeleteMolecule;

        public void ShowAtomsLinkingDialog()
        {
         /*   ILinkingDialogView linkingDialogView = _linkingDialogViewInjector.Get();
            linkingDialogView.ShowLinkingDialog();*/
        }

        private void transformMoleculeButton_Click(object sender, EventArgs e)
        {
          /*  if (_moleculeTransformHandler.IsActive)
            {
                _moleculeTransformHandler.StopHandle();
                return;
            }
            _moleculeTransformHandler.MoleculeDrawer = _moleculesDisplayView.GetMoleculeDrawerForMolecule(_mainModel.CurrentMolecule);
            if (_moleculeTransformHandler.MoleculeDrawer != null)
            {
                _moleculeTransformHandler.StartHandle();
            }*/
        }
    }
}